<?php
// supporting classes:
require_once _BLIBPATH . 'utility/log.php';
/**
 * the source of evil
 * the class that rules classes
 * the lord of rings! err... classes.
 * alright, infact, it provides:
 * >> log functionalities
 * >> bench marking functionalities
 * >> and more to come!
 *
 * all classes extend this one so we can have cnetralised control lib for all!
 * simple as that
 * @abstract o'course, you dont want to use this thingy directly, it's gotta be abstract
 * @uses Blib_Utility_Log
 * @author bbqq <toughgamer@gmail.com>
 * @package com.blib.core
 * @copyright bbqq qstufie.com
 */
abstract class Blib_Class_Atomic
{
    /**
     * the log file path...
     * so we can use the log directly
     * @var str
     */
    protected $_LOGFILE = null;


    /**
     * internal var for log
     * @var Blib_Utility_Log
     */
    private $_logHandle = null;


    /**
     * cache hanlder
     * @var Blib_Cache_Manager
     */
    private $_cacheHandle = null;


    /**
     * get the log handle
     * @return $this
     */
    private function _getLogHandle()
    {
        // safe checking, maybe we dont want log?
        if (empty($this->_LOGFILE)) {
            return false;
        }

        if (!$this->_logHandle instanceof Blib_Utility_Log) {
            $this->_logHandle = new Blib_Utility_Log($this->_LOGFILE);
        }

        return $this->_logHandle;

    }// end _getLogHandle


    /**
     * log the stuff down
     * @param str $message the actual log message
     * @param str $type type of log, output|warning|error|fatal
     * @param bool $save if true, save it right away
     * @param bool $createFile if true, create the file if not exist...
     * @param bool $echo if true, also echo the log...
     * @return self
     */
    final protected function _log($msg, $type = 'output', $save = true, $createFile = true, $echo = true)
    {
        // safe checking, maybe we dont want log?
        if (empty($this->_LOGFILE)) {
            return $this;
        }

        // u may want to log msg array...
        if (is_array($msg)) {
            $msg = implode("\n", $msg);
        }

        // compose message:
        $msg = "[$type]    $msg";

        // if file doesn't exist, create it...
        if (!file_exists($this->_LOGFILE) && $createFile) {
            if (!@touch($this->_LOGFILE)) {
                throw new Blib_Exception('Could not create log file: ' . $this->_LOGFILE);
            }
        }

        $this->_getLogHandle()->log($msg, $save, $echo);
        return $this;

    }// end _log


    /**
     * echo the log message out
     * @param str $msg the message
     * @param str $type the type of log
     * @return $this
     */
    protected function _echoLog($msg, $type)
    {
        echo $this->_composeLog($msg, $type);
        return $this;

    }// end _echoLog


    /**
     * compose the log message
     * @param str $message the actual log message
     * @param str $type type of log, output|warning|error|fatal
     * @return str the composed message
     */
    final protected function _composeLog($msg, $type = 'output')
    {
        // obscure the message if there is password in it...
        $msg =  preg_replace('/password="([^"><]*)"/mi', 'password="*******"', $msg);

        // safe checking, maybe we dont want log?
        if (empty($this->_LOGFILE)) {
            return $msg;
        }

        return $this->_getLogHandle()->composeLog("[$type]    $msg");

    }// end _composeLog


    /**
     * save the log
     */
    final protected function _saveLog()
    {
        // safe checking, maybe we dont want log?
        if (empty($this->_LOGFILE)) {
            return $this;
        }

        $this->_getLogHandle()->save();
        return $this;

    }// end _saveLog


    /**
     * get configuration value out
     * @param str $key the key
     * @param mixed $default the default value if not found...
     * @return mixed the config value
     */
    final public function getConfig($key, $default = null)
    {
        return Blib::getConf($key, null, $default);

    } // end getConfig


    /**
     * get the cache handler
     * @return Blib_Cache_Manager
     */
    private function _getCacheHandle()
    {
        if (!$this->_cacheHandle instanceof Blib_Cache_Manager) {
            $this->_cacheHandle = new Blib_Cache_Manager;
        }

        return $this->_cacheHandle;

    }// end _getCacheHandle


    /**
     * caching delegate
     * @param str $key the key to cache
     * @param str $data the data to store
     * @param int $expiry if exists, use it to detect when to expire.
     */
    final public function saveCache($key, $data, $expiry = null)
    {
        return $this->_cacheHandle->cache($key, $data, $expiry);

    }// end saveCache


    /**
     * load cached data by key
     * @param str $key the key
     * @return mixed
     */
    final public function loadCache($key)
    {
        return $this->_cacheHandle->load($key);

    }// end loadCache


    /**
     * remove cache by key if required
     * @return bool
     */
    final public function purgeCache($key)
    {
        return $this->_cacheHandle->purge($key);

    }// end purgeCache


    /**
     * set log path
     * @param str $path path to the log
     * @return void
     */
    final protected function _setLogPath($path)
    {
        $this->_LOGFILE = $path;

    }// end _setLogPath


    /**
     * dump the class itself
     * @return void
     */
    final public function dump()
    {
        Blib::dump($this);

    }// end dump

}// end Blib_Class_Atomic
?>